package com.yuerenjf.yuerenjinfu.ads;

import java.util.ArrayList;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.yuerenjf.yuerenjinfu.MyApplication;

/**
 * @Author: zhaocd 
 * 2016-5-11下午1:24:03 
 * description:广告数据库操作类
 */
public class AdvertisementDao {
	private static DBHelper mHelper = DBHelper.getInstance(MyApplication.getContext());;
	private static String TAG = "AdvertisementDao";

	public synchronized static void insertAd(Ad adver) {
		try {
			//if(!isExists(adver)){//某条广告如果存在，则不进行重复插入
				SQLiteDatabase db = mHelper.getWritableDatabase();
				db.execSQL(
						"insert into "
								+ DBHelper.TABLE_NAME_ADVER
								+ "(ad_id,end,title,url,typeid,androidimg,flag) values(?,?,?,?,?,?,?)",
						new Object[] { adver.getId(),adver.getEnd(),adver.getTitle(),adver.getUrl(),adver.getTypeid(),adver.getAndroidimg(),adver.getFlag()
								 });
				db.close();
			//}
		} catch (Exception e) {
		}
	}

	public synchronized static void deleteAd(Ad adver) {
		try {
			SQLiteDatabase db = mHelper.getWritableDatabase();
			db.delete(DBHelper.TABLE_NAME_ADVER, "ad_id=?", new String[]{adver.getId()});
			db.close();
		} catch (Exception e) {
		}
	}

	/**
	 * 获得医生信息
	 * 
	 * @return
	 */
	public synchronized static ArrayList<Ad> getAdDataList() {
		ArrayList<Ad> list = new ArrayList<Ad>();
		Cursor cursor = null;
		try {
			SQLiteDatabase db = mHelper.getReadableDatabase();
			cursor = db.rawQuery("select * from " + DBHelper.TABLE_NAME_ADVER
					+ " ", null);
			while (cursor.moveToNext()) {
				Ad info = new Ad();
				info.setId(cursor.getString(cursor.getColumnIndex("ad_id")));
				info.setEnd(cursor.getString(cursor.getColumnIndex("end")));
				info.setTitle(cursor.getString(cursor.getColumnIndex("title")));
				info.setUrl(cursor.getString(cursor.getColumnIndex("url")));
				info.setTypeid(cursor.getString(cursor.getColumnIndex("typeid")));
				info.setAndroidimg(cursor.getString(cursor.getColumnIndex("androidimg")));
				info.setFlag(cursor.getString(cursor.getColumnIndex("flag")));
				list.add(info);
			}
			cursor.close();
			db.close();
		} catch (Exception e) {
		}
		return list;

	}

	public synchronized static boolean isExists(Ad adver) {
		boolean exists = false;
		try {
			SQLiteDatabase db = mHelper.getReadableDatabase();
			Cursor cursor = db.rawQuery("select * from " + DBHelper.TABLE_NAME_ADVER
					+ " where ad_id = ? ", new String[] { adver.getId() });
			exists = cursor.moveToNext();
			cursor.close();
			db.close();
		} catch (Exception e) {
		}
		return exists;
	}
}